package com.perfma.xtest.boss;

import com.alibaba.fastjson.JSON;
import com.atlassian.jira.rest.client.api.IssueRestClient;
import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.RestClientException;
import com.atlassian.jira.rest.client.api.domain.BasicIssue;
import com.atlassian.jira.rest.client.api.domain.input.IssueInput;
import com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;

import java.io.IOException;
import java.net.URI;
import java.util.Date;

/**
 * @author: junhui.si
 * @date: 2021-01-06 19:31
 **/
public class JiraHelper {

    private static final String JIRA_LINE_BREAK = "\n";

    public static void createIssue() throws IOException {
        String url = "http://jira.perfma-inc.net";
        String username = "mingfeng.lu";
        String password = "mingfeng.lu";
        AsynchronousJiraRestClientFactory asynchronousJiraRestClientFactory = new AsynchronousJiraRestClientFactory();
        JiraRestClient jiraRestClient = asynchronousJiraRestClientFactory.createWithBasicHttpAuthentication(URI.create(url), username, password);

        IssueRestClient issueRestClient = jiraRestClient.getIssueClient();
        jiraRestClient.close();

        // 拼接问题描述
        StringBuilder descriptionSb = new StringBuilder();
        String description = "<ol><li>第三方士大夫的</li><li>水电费第三方是</li></ol><p>水电费第三方的份上</p><ul><li>氛围防风网</li></ul><p>违反为纷纷为</p><pre class=\"ql-syntax\" spellcheck=\"false\">ss'd'f'd'f's'f'd  对方水电费第三方第三方's\ns省的翻大杀四方\n</pre><p><br></p><blockquote>神鼎飞丹砂发的是</blockquote><blockquote>神鼎飞丹砂发的是</blockquote><blockquote>水电费发送到发送到</blockquote><h1>是大V师傅的</h1><h1>是非法的是</h1><p>委屈二<sup>12</sup>3331 水电费</p><p class=\"ql-align-right ql-direction-rtl\">发的、、大幅度发顺丰递四方速递</p><p class=\"ql-align-right ql-direction-rtl\">地方大丰收地方神鼎飞丹砂</p><p>水电费水电费是</p><p><br></p><p class=\"ql-align-center\">水电费电风扇</p><p>水电费地方都死</p><p><br></p><p>地方上档次东方闪电</p>";
        String solution = "<h1>1</h1><h2>2</h2><p>3</p><p><s>4</s></p><p><span class=\"ql-size-small\">5</span></p><p><span class=\"ql-size-huge\">6</span></p><h1>7</h1><h6>8</h6><p>9</p><h3>10</h3><p><span class=\"ql-font-serif\">11</span></p><p><span class=\"ql-font-serif\">12</span></p>";
        descriptionSb.append("h1. 问题描述：").append(JIRA_LINE_BREAK).append(description)
                .append(JIRA_LINE_BREAK).append(JIRA_LINE_BREAK).append(JIRA_LINE_BREAK)
                .append("h1. 解决方案：").append(JIRA_LINE_BREAK).append(solution);

        String newDescription = descriptionSb.toString();
        // 替换换行符
        newDescription = replaceLineBreak(newDescription);
        // 去掉富文本标签
        newDescription = clearHtmlTags(newDescription);

        IssueInput issueInput = new IssueInputBuilder()
                // 项目
                .setProjectKey("XSEA")
                // 问题类型
                .setIssueTypeId(10002L)
                // 概要
                .setSummary("这是概要"+ DateUtils.formatDate(new Date()))
                // 描述
                .setDescription(newDescription)
                .build();
        BasicIssue basicIssue = issueRestClient.createIssue(issueInput).claim();
        try {
            System.out.println(JSON.toJSONString(basicIssue));
        } catch (RestClientException e) {
            System.out.println(e.getStatusCode());
            System.out.println(e.getErrorCollections());
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println(e.toString());
            System.out.println(e.getMessage());
        }
    }

    public static void main(String[] args) throws IOException {
        createIssue();
    }

    /**
     * 替换换行符
     *
     * @param str 字符串
     * @return 结果
     */
    private static String replaceLineBreak(String str) {
        return str.replace("</p>", JIRA_LINE_BREAK)
                .replace("</li>", JIRA_LINE_BREAK)
                .replace("</pre>", JIRA_LINE_BREAK)
                .replace("<br>", JIRA_LINE_BREAK)
                .replace("</span>", JIRA_LINE_BREAK)
                .replace("</blockquote>", JIRA_LINE_BREAK)
                .replace("</h1>", JIRA_LINE_BREAK)
                .replace("</h2>", JIRA_LINE_BREAK)
                .replace("</h3>", JIRA_LINE_BREAK)
                .replace("</h4>", JIRA_LINE_BREAK)
                .replace("</h5>", JIRA_LINE_BREAK)
                .replace("</h6>", JIRA_LINE_BREAK)
                ;
    }

    public static String clearHtmlTags(String htmlStr) {
        //定义script的正则表达式，去除js可以防止注入
        String scriptRegex = "<script[^>]*?>[\\s\\S]*?<\\/script>";
        //定义style的正则表达式，去除style样式，防止css代码过多时只截取到css样式代码
        String styleRegex = "<style[^>]*?>[\\s\\S]*?<\\/style>";
        //定义HTML标签的正则表达式，去除标签，只提取文字内容
        String htmlRegex = "<[^>]+>";

        // 过滤script标签
        htmlStr = htmlStr.replaceAll(scriptRegex, "");
        // 过滤style标签
        htmlStr = htmlStr.replaceAll(styleRegex, "");
        // 过滤html标签
        htmlStr = htmlStr.replaceAll(htmlRegex, "");
        return htmlStr;
    }
}
